﻿using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Image2Pdf
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


        }

        private void panel1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))    //判断拖来的是否是文件
                e.Effect = DragDropEffects.Link;                //是则将拖动源中的数据连接到控件
            else e.Effect = DragDropEffects.None;
        }

        private void panel1_DragDrop(object sender, DragEventArgs e)
        {
            StringBuilder filesName = new StringBuilder("");
            Array file = (System.Array)e.Data.GetData(DataFormats.FileDrop);//将拖来的数据转化为数组存储

            foreach (string item2 in file)
            {
               var  item = item2.ToLower();

//只支持jpg，简单需要其他再说吧。
                if (item.EndsWith("jpg"))
                {
                   

                    PdfDocument pdf = new PdfDocument(new PdfWriter(item.Replace(".jpg", "") + ".pdf"));
                    Document document = new Document(pdf);
                    var pic = iText.IO.Image.ImageDataFactory.Create(item);
                    document.Add(new iText.Layout.Element.Image(pic));
                    document.Close();
                }
            }

        }
    }
}
